export const state = () => ({
    signIn: true,
    account: '0',
    token:''
})

export const mutations = {
    setSignIn(state, signIn) {
        state.signIn = signIn
    },
    setAccount(state, account) {
        state.account = account
    },
    setToken(state, token) {
        state.token = token
    }
}

export const actions = {
    postAuth({}, { auth_id, password }) {
        return this.$api.account.postAuth(auth_id, password).then((res) => {
            if(res.data.code == 200) {
                return Promise.resolve(res.data.data)
            }
            else {
                this.$router.push("/mainPage/error")
                return Promise.reject(new Error(res.data.msg))
            }
        }, (error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        }).catch((error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        })
    },
    postToken({}) {
        return this.$api.account.postToken().then((res) => {
            if(res.data.code == 200) {
                return Promise.resolve(res.data.data)
            }
            else {
                this.$router.push("/mainPage/error")
                return Promise.reject(new Error(res.data.msg))
            }
        }, (error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        }).catch((error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        })
    },
    signOut({commit}) {
        commit('setSignIn', false)
        commit('setAccount', null)
        commit('setToken','')
        localStorage.removeItem('token')
    },
    signIn({commit},{auth_id, password}){
        return this.$api.account.postAuth( auth_id, password).then((res) => { 
            if(res.data.code === 200 ) {
                commit('setToken',res.data.data.token)
                return this.$api.account.postToken().then((res)=>{
                    if(res.data.code==200&&res.data.msg =='admin')
                    {
                        localStorage.setItem('token', res.data.data.token)
                        return Promise.resolve('OK')
                    }
                    else if(res.data.code==200&& res.data.msg=='student')
                    {
                        this.$router.push('/stuPage/stuDataPage')
                        return Promise.resolve('OK')
                    }
                    else
                    {
                        return Promise.reject(new Error(res.data.msg))
                    }
                },(error)=>{
                    return Promise.reject(error)
                }).catch((error)=>{
                    return Promise.reject(error)
                })
            }
            else
            {
                return Promise.reject(new Error("请输入正确的管理员账号与密码!"))
            }
        },(error)=>{
            return Promise.reject(error)
        }).catch((error)=>{
            return Promise.reject(error)
        })
    }
}